#!/usr/bin/env python3

import dash
import dash_bootstrap_components as dbc
import pandas as pd

from .utils import db_opt as do
from . import styles
from dash import dcc

dash.register_page(__name__, path_template="/Student/<student_id>")

def get_course_list(sid):
  c_info = do.DatabaseOperation().get_sc_for_student(sid)

  get_param = lambda arg: [i[arg] for i in c_info]
  df = pd.DataFrame({
    "course_id":   get_param("id"),
    "course_name": get_param("name"),
    "score":       get_param("score")
  })
  return dash.html.Div(
    dbc.Table.from_dataframe(df, striped=True, bordered=True, hover=True),
  )

def generate_student_badge(**kwargs):
  return [dbc.Badge("{}: {}".format(k, v), className="me-1", color="success") for k, v in kwargs.items()]

def layout(student_id=None, **kwargs):
  stu_info, = do.DatabaseOperation().get_target_student(student_id)
  return dbc.Container(
    [
      dash.html.Img(src=dash.get_asset_url("student_img.png"), width=250,
                    className="rounded-circle mx-auto d-flex img-thumbnail"),
      dash.html.H2(stu_info["name"]),
      dash.html.Div(generate_student_badge(**stu_info)),
      dash.html.Hr(),
      get_course_list(student_id)
    ],
    style={"textAlign": "center"}
  )
